In [17]:
#Import packages.
import numpy as np
from matplotlib import pyplot as plt
In [41]:
#Define constants in the problem.
#Permittivity of free space, epsilon_0 [in C^2/Nm^2].
epsilon_0 =8.854 * 10**(-12)
#Constant coefficient, k.
k = 1/(4 * np.pi * epsilon_0)
#Test charge.
q = 1
#Charges.
q_1 = 2
q_2 = -2
#Distance from origin.
a = 3
#b = ? (One can add a different distance for a non-symmetric problem. This consideration only makes a difference if there are more than two charges though.)
In [55]:
#Define an area where we want to look at the force.
n = 8
X, Y = np.mgrid[-n:n, -n:n] + 0.1
#X[n - a, 0] = a + 1
#X[n + a, 0] = a + 1
#Y[n - a, 0] = a + 1
#Y[n + a, 0] = a + 1
#Magnitude of radius from charges 1 and 2 to the point where the field is being measured.
r_1 = np.sqrt((X + a)**2 + Y**2)
r_2 = np.sqrt((X - a)**2 + Y**2)
In [56]:
#Force equation.
F_x = (k * q * q_1/r_1**(3/2) * (X + a)) + (k * q * q_2/r_2**(3/2) * (X - a))
F_y = (k * q * q_1/r_1**(3/2) * (Y)) + (k * q * q_2/r_2**(3/2) * (Y))
#This breaks F(x,y) into two parts, F(x,y) = ( F_x(x,y), F_y(x,y) ).
In [57]:
fig = plt.figure(figsize = (18,18))
plt.quiver(X, Y, F_x, F_y, alpha = 0.9,)
#Plot the location of the two charges.
plt.plot((-a, a), (0, 0), linestyle = 'None', marker = 'o', markersize = 16, color = 'darkorange')
plt.show()
In [58]:
#Calculate the magnitude of the force.
F = np.sqrt(F_x**2 + F_y**2)
fig = plt.figure(figsize = (18,18))
#Plot the vector field. This time the magnitude of the force will be displayed in two different ways, by color of the arrow and by size.
plt.quiver(X, Y, F_x, F_y, F, alpha = 0.9)
#Plot the location of the two charges.
plt.plot((-a, a), (0, 0), linestyle = 'None', marker = 'o', markersize = 16, color = 'darkorange')
plt.show()